/** @file vxp-state.c
* Declares a data structure and related functions to store the lexer's state.
* Because vxp is a pure-parser (in Bison-speak), it cannot use any global
* variables. Using global variables with Flex, while running as PHP module
* under Apache, causes the parser to complain in certain cases, when 
* information about the previously parsed token was used.
* To solve this, the state is kept in struct vxpState and can be accessed
* using the following two functions provided by Flex for keeping user-defined
* data: vxp_set_extra() and vxp_get_extra().
*
* @author Victor Petrov <victor_petrov@harvard.edu>
* @date   July 23, 2011
* @cond
*------------------------------------------------------------------------------
* LEGAL:
*------------------------------------------------------------------------------
*
* Copyright (c) 2011, The President and Fellows of Harvard College
* Copyright (c) 2011, The Neuroinformatics Research Group at Harvard University
* All rights reserved.
*
* License: New BSD License   <http://creativecommons.org/licenses/BSD/>
*-------------------------
* Redistribution and use in source and binary forms, with or without 
* modification, are permitted provided that the following conditions are met:
*
*	Redistributions of source code must retain the above copyright notice, 
*	this list of conditions and the following disclaimer.
*
*	Redistributions in binary form must reproduce the above copyright notice, 
*	this list of conditions and the following disclaimer in the documentation
*	and/or other materials provided with the distribution.
*
*	Neither the name of the Neuroinformatics Research Group at Harvard, nor 
*	the names of its contributors may be used to endorse or promote products 
*	derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
* POSSIBILITY OF SUCH DAMAGE.
*------------------------------------------------------------------------------
* vim: ts=4
*------------------------------------------------------------------------------
* @endcond
*/

#include "vxp-state.h"
#include <assert.h>
#include "errors.h"

/* PHP extension include files*/
#ifdef PHPEXT
	#include "php.h"
	#include "php_vxp.h"
#endif

#ifndef PHPEXT
    /** Zend macros */
    #define ecalloc(x,y) calloc((x),(y))
    #define erealloc(x,y) realloc((x),(y))
    #define emalloc(x) malloc(x)
    #define efree(x) free(x)
#endif

/** Creates a new vxpState struct.
* @return A pointer to the newly allocated vxpState (not zero-filled)
*/
struct vxpState* newState(struct MemBlockInfo* mem)
{
	struct vxpState* result=NULL;               /* result container */

    assert(mem);                                /* check mem */

	result=emalloc(sizeof(struct vxpState));    /* allocated new state */

	if (!result)                                /* check for errors */
		memerror(sizeof(struct vxpState),__func__,__FILE__,__LINE__,mem);

	return result;                              /* return the result */
}

/** Sets default values for all members of struct vxpState.
* @param[in] state A pointer to the vxpState instance to change (if *state is
*                  NULL, a new vxpState will be allocated.
* @param[in] mem   A pointer to the MemBlockInfo struct.
*/
void initState(struct vxpState** state, struct MemBlockInfo* mem)
{
	assert(state);                  /* check state */
    assert(mem);                    /* check mem */

	if (!*state)                    /* if not a pointer to a valid state */
		*state=newState(mem);       /* allocate new state */

	(*state)->lastToken=0;          /* set last token to 0 */
	(*state)->mem=mem;              /* set the MemBlockInfo instance */
	(*state)->quoteType=NO_QUOTES;  /* set default quote type */
	(*state)->errorKind=VXP_ERROR;  /* set default error kind */
}

